<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div id="box">
    <input type="text" oninput="shuru()" placeholder="输入用户名">
    <div id="show"> </div>
  </div>
  <script>

    function shuru(e) {

      var game = {
        name: "王者荣耀",
        player: "初始用户名",
        area: "比尔吉沃特"
      }
      // 

      var e = e || window.event;
      var val = e.target.value; //输入框的值
      var temp = null;
      Object.defineProperty(game, "player", {
        get: function () {
          return temp
        },
        set: function (playerVal) {
          if (playerVal == "") {
            alert("不能为空")
          } else if (/tmd/g.test(playerVal)) {

            temp = playerVal.replace(/tmd/g, "***")
          } else if (!(/^\w{2,6}$/.test(playerVal))) {
            alert("用户名长度不符合规则")
          } else {
            temp = playerVal
          }
        },
        configurable: false //能否被删除
      })

      game.player = val; //赋值用户名
      var show = document.getElementById("show");
      show.innerHTML = game.player;
    }





  </script>
</body>

</html>